//
//  CustomerOrderDetailViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/9/18.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "CustomerOrderDetailViewController.h"
#import "BLOrder.h"
#import "BLHeader.h"
#import "PayViewController.h"
#import "OrderDetailNet.h"
#import "PayOrderNet.h"

@interface CustomerOrderDetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *topOrderCode;
@property (weak, nonatomic) IBOutlet UILabel *orderStateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *orderStateImage;
@property (weak, nonatomic) IBOutlet UIView *customView;
@property (weak, nonatomic) IBOutlet UILabel *customNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *customTelLabel;
@property (weak, nonatomic) IBOutlet UILabel *customAddressLabel;
@property (weak, nonatomic) IBOutlet UILabel *saleNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *saleTelLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderCodeLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderThemeLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderDesLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderTimeLabel;
@property (weak, nonatomic) IBOutlet UIButton *orderPayBtn;
@property (weak, nonatomic) IBOutlet UIView *storeView;
@property (weak, nonatomic) IBOutlet UILabel *storeNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *storeTelLabel;
@property (weak, nonatomic) IBOutlet UILabel *storeAddressLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCustom;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightStore;
@property (weak, nonatomic) IBOutlet UIButton *defuseOrder;

@end

@implementation CustomerOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.order) {
        [self setupUI];
    } else {
        [self getOrderDetail];//推送通知进来
    }
}

- (void)getOrderDetail {
    [OrderDetailNet getOrderDetail:self.orderCode completer:^(id result, NSError *error) {
        self.order = result;
        [self setupUI];
    }];
}

- (void)setupUI {
    self.heightStore.constant = 105;
    self.heightCustom.constant = 105;
    [self.storeView setHidden:NO];
    [self.customView setHidden:NO];
    self.title = @"订单详情";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    NSString *userClass = [[NSUserDefaults standardUserDefaults] objectForKey:USERCLASS];
    switch ([self.order.orderState integerValue]) {
        case 1:// 已交单
            self.orderStateLabel.text = @"已下单";
            self.orderStateImage.image = [UIImage imageNamed:@"orderState1"];
            if ([userClass integerValue] == 1) {
                [self.orderPayBtn setTitle:@" 接受订单 " forState:UIControlStateNormal];
            }else {
                self.orderPayBtn.hidden = YES;
                self.defuseOrder.hidden = YES;
            }
            break;
        case 2:// 已接单
            self.orderStateLabel.text = @"已接单";
            self.orderStateImage.image = [UIImage imageNamed:@"orderState2"];
            self.defuseOrder.hidden = YES;
            if ([userClass integerValue] != 3) {
                self.orderPayBtn.hidden = YES;
            }
             [self.orderPayBtn setTitle:@" 立即支付 " forState:UIControlStateNormal];
            break;
        case 3://已付款
            self.orderStateLabel.text = @"已付款";
            self.orderStateImage.image = [UIImage imageNamed:@"orderState3"];
            if ([userClass integerValue] != 3) {
                self.orderPayBtn.hidden = YES;
            } else {
                if ([self.order.payStep intValue] == [self.order.payWay integerValue]) {
                    [self.orderPayBtn setTitle:@" 确定完成 " forState:UIControlStateNormal];
                }else {
                    self.orderStateLabel.text = [NSString stringWithFormat:@"已付第%@期",self.order.payStep];
                    [self.orderPayBtn setTitle:@" 立即支付 " forState:UIControlStateNormal];
                }
            }
            self.defuseOrder.hidden = YES;
            break;
        case 4://已完成
            self.orderStateLabel.text = @"已完成";
            self.orderStateImage.image = [UIImage imageNamed:@"orderState4"];
            self.orderPayBtn.hidden = YES;
            self.defuseOrder.hidden = YES;
            break;
        case -1://已拒单
            self.orderStateLabel.text = @"已拒单";
            self.orderStateImage.hidden = YES;
            self.orderPayBtn.hidden = YES;
            self.defuseOrder.hidden = YES;
            break;
        default:
            break;
    }
    
    self.customNameLabel.text = [NSString stringWithFormat:@"客户姓名：%@",self.order.customerName];
    self.customTelLabel.text = [NSString stringWithFormat:@"客户电话：%@",self.order.customerTel];
    self.customAddressLabel.text =  [NSString stringWithFormat:@"客户地址：%@",self.order.deliveryAddress];
    self.saleNameLabel.text = [NSString stringWithFormat:@"订单销售：%@",self.order.saleName];
    self.saleTelLabel.text = [NSString stringWithFormat:@"订单销售：%@",self.order.saleTel];
    self.orderCodeLabel.text = [NSString stringWithFormat:@"订单编号：%@",self.order.orderCode];
    self.topOrderCode.text = [NSString stringWithFormat:@"订单编号：%@",self.order.orderCode];
    self.orderThemeLabel.text = self.order.orderTitle;
    self.orderPriceLabel.text = [NSString stringWithFormat:@"￥ %@",self.order.orderPrice];
    self.orderDesLabel.text = [NSString stringWithFormat:@"订单描述：%@",self.order.orderInfo];
    self.orderTimeLabel.text = self.order.orderTime;
    
    self.orderPayBtn.layer.cornerRadius = 5;
    self.orderPayBtn.layer.borderColor = kAppThemeColor.CGColor;
    self.orderPayBtn.layer.borderWidth = 1;
    self.orderPayBtn.layer.masksToBounds = YES;
    self.defuseOrder.layer.cornerRadius = 5;
    self.defuseOrder.layer.borderWidth = 1;
    self.defuseOrder.layer.borderColor = kAppThemeColor.CGColor;
    self.defuseOrder.layer.masksToBounds = YES;
    
    self.storeNameLabel.text = [NSString stringWithFormat:@"店主：%@",self.order.supplierName];
    self.storeTelLabel.text = [NSString stringWithFormat:@"店主电话：%@", self.order.supplierTel];
    self.storeAddressLabel.text = [NSString stringWithFormat:@"店主地址：%@", self.order.supAddress];
    self.orderDesLabel.numberOfLines = 0;
    
    if ([userClass integerValue] == 1 && [self.order.orderState integerValue] < 3) {
        [self.customView setHidden:YES];
        self.heightCustom.constant = 0;
    } else if ([userClass integerValue] == 3 && [self.order.orderState integerValue] < 3) {
        [self.storeView setHidden:YES];
        self.heightStore.constant = 0;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payNotification) name:@"payNotification" object:nil];
    
     NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:@"userDic"]];
    if (self.order) {
        [dic removeObjectForKey:self.order.orderCode];
    }else {
        [dic removeObjectForKey:self.orderCode];
    }
    [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"userDic"];
}

- (void)payNotification {
     self.orderCode = self.order.orderCode;
     [self getOrderDetail];
     [[NSNotificationCenter defaultCenter] postNotificationName:@"updateOrderList" object:nil];
}

- (IBAction)payOrder:(id)sender {
     NSString *userClass = [[NSUserDefaults standardUserDefaults] objectForKey:USERCLASS];
    __weak typeof (self) weakSelf = self;
    if([self.order.orderState integerValue] == 2 && [userClass integerValue] == 3) {
        PayViewController *controller = [[PayViewController alloc] init];
        controller.order = self.order;
        [self.navigationController pushViewController:controller animated:YES];
    }else if ([self.order.orderState integerValue] == 1 && [userClass integerValue] == 1){//接收订单
        [PayOrderNet updateOrder:self.order.orderId orderState:@"2" completer:^(id result, NSError *error) {
            if (result) {
                weakSelf.order = result;
                [weakSelf setupUI];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"updateOrderList" object:nil];
            }
        }];
    }else {
        // 分期付款 付完第一期后的  第二期或者第三期的付款
        if ([self.order.orderState integerValue] == 3 && [self.order.payWay integerValue] != [self.order.payStep integerValue]) {
            PayViewController *controller = [[PayViewController alloc] init];
            controller.order = self.order;
            [self.navigationController pushViewController:controller animated:YES];
            return;
        }
        [PayOrderNet updateOrder:self.order.orderId orderState:@"4" completer:^(id result, NSError *error) {
            if (result) {
                weakSelf.order = result;
                [weakSelf setupUI];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"updateOrderList" object:nil];
            }
        }];
    }
}

#pragma mark 拒绝订单
- (IBAction)defuseOrder:(UIButton *)sender {
     __weak typeof (self) weakSelf = self;
    [PayOrderNet updateOrder:self.order.orderId orderState:@"-1" completer:^(id result, NSError *error) {
        if (result) {
            weakSelf.order = result;
            [weakSelf setupUI];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"updateOrderList" object:nil];
        }
    }];
}

@end
